package listeners;

import playlistData.PlaylistManager;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.Toast;

import com.unipv14.androidgallery.CurrentImageActivity;
import com.unipv14.androidgallery.IListsRefresher;
import com.unipv14.androidgallery.ImageShower;

import fileManagers.FileListGetter;
import fileManagers.FileUtilities;
import fileManagers.FilterImage;

public class FileItemListener implements IListener,
		OnClickListener {

	private FileListGetter getter;
	private PlaylistManager manager;
	private IListsRefresher listRefresher;
	private Activity mainActivity;
	private String selectedFile;
	public static final String[] FOLDER_OPTION_MENU=new String[]{
		
	};
	public static final String[] IMAGE_OPTION_MENU=new String[]{
		"Remove from playlist", "Show Image"
	};
	
	
	public FileItemListener(Activity mainActivity,
			IListsRefresher listRefresher, FileListGetter getter,
			PlaylistManager manager) {
		
		this.listRefresher = listRefresher;
		this.mainActivity = mainActivity;
		this.getter = getter;
		this.manager = manager;
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		try {
			if (which == 1) {
				selectedFile = FileUtilities.nameToPath(getter, selectedFile);
				ImageShower.showImage(getListPath(), mainActivity,selectedFile,CurrentImageActivity.class);
			} else if (which == 0) {
				if (manager.isPlaylistList()) {
					Toast.makeText(mainActivity, "select a playlist, first",
							Toast.LENGTH_SHORT).show();
				} else {
					manager.addImage(
							FileUtilities.nameToPath(getter, selectedFile),
							selectedFile);
					listRefresher.refreshList();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public String getFolder(String name) {
		return getter.getDirectory();
	}

	@Override
	public String[] getMenuOptions(String name) {
		if (new FilterImage().checkFilter(name, getter.getDirectory())) {
			return new String[] { "Add to playlist", "Show image" };
		} else {
			return new String[] {};
		}
	}

	@Override
	public void goToSubDirectory(String folder) {
		getter.goToSubDirectory(folder);
	}
	
	@Override
	public void setSelectedFile(String selectedFile) {
		this.selectedFile=selectedFile;
	}

	@Override
	public String[] getListPath() {	
		return getter.getPathList();
	}
}
